package org.csu.jpetstore.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.jpetstore.entity.Product;
import org.csu.jpetstore.mapper.ProductMapper;
import org.csu.jpetstore.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;

    @Override
    public List<Product> getProductListByCategoryId(String categoryId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        return productMapper.selectList(queryWrapper);
    }

    @Override
    public List<Product> getProductListByProductIdAndName(String keyword) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("product_id", keyword);
        List<Product> productList1 = productMapper.selectList(queryWrapper);
        QueryWrapper<Product> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.like("name", keyword);
        List<Product> productList2 = productMapper.selectList(queryWrapper1);
        //两个list取并集操作
        productList2.removeAll(productList1);
        productList2.addAll(productList1);
        return productList2;
    }


    @Override
    public Product getProduct(String productId) {
        return productMapper.selectById(productId);
    }

    @Override
    public void createNewProduct(String productId, String categoryId, String name, String image, String description) {
        Product product = new Product();
        product.setProductId(productId);
        product.setCategoryId(categoryId);
        product.setName(name);
        product.setImage(image);
        product.setDescription(description);
        productMapper.insert(product);
    }


}
